package com.chzsyh.evwer.ui.web;

import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;

import androidx.annotation.Nullable;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.owbtgad.common.base.FragmentActivity;
import com.owbtgad.common.utils.ARouterString;
import com.owbtgad.common.utils.ActivityUtils;

@Route(path = ARouterString.WEB_ACTIVITY)
public class WebActivity extends FragmentActivity<WebFragment> {
    private static String TAG = "WebActivity";
    @Autowired(name = "url")
    String mUrl;
    @Autowired(name = "title")
    String mTitle;
    private WebFragment mWebFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        ARouter.getInstance().inject(this);
        super.onCreate(savedInstanceState);
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        try {
            if (Integer.parseInt(android.os.Build.VERSION.SDK) >= 11) {
                getWindow()
                        .setFlags(
                                android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
                                android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED
                        );
            }
        } catch (Exception e) {
        }
        mUrl = mUrl.replaceAll(ActivityUtils.KEY_WEB_AND,"&");
        mUrl = mUrl.replaceAll(ActivityUtils.KEY_WEB_AND2,"#");
        Log.d(TAG, "onCreate:startWebViewActivity= "+mUrl);
        mWebFragment = WebFragment.newInstance(mUrl, mTitle);
        showFragment(mWebFragment);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        mUrl = intent.getStringExtra("url");
        Log.d(TAG, "onNewIntent: " + mUrl);
        if (mWebFragment != null) {
            mWebFragment.setUrl(mUrl);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Log.d(TAG, "onKeyDown: ");
            if (mWebFragment != null) {
                Log.d(TAG, "onKeyDown: 2");
                mWebFragment.mLeftClick();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
